{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "882ece37",
   "metadata": {},
   "source": [
    "### python script for docker\n",
    "这是一个python脚本，用于解决为docker container新增挂载点和添加端口映射的繁杂步骤问题\n",
    "\n",
    "**步骤**\n",
    ">1. 使用`docker info | grep 'Root'`查看容器存放目录,可以借助python的subprocess模块实现调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "36e1ea94",
   "metadata": {},
   "outputs": [
    {
     "ename": "UnicodeDecodeError",
     "evalue": "'utf-8' codec can't decode byte 0xc7 in position 1: invalid continuation byte",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m                        Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[15], line 44\u001b[0m\n\u001b[0;32m     40\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m shell_encoding\n\u001b[0;32m     41\u001b[0m cmd_output \u001b[38;5;241m=\u001b[39m subprocess\u001b[38;5;241m.\u001b[39mcheck_output(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdir\u001b[39m\u001b[38;5;124m\"\u001b[39m, shell \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m---> 44\u001b[0m \u001b[38;5;28mprint\u001b[39m (cmd_output\u001b[38;5;241m.\u001b[39mdecode(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mutf8\u001b[39m\u001b[38;5;124m'\u001b[39m))\n\u001b[0;32m     45\u001b[0m sys\u001b[38;5;241m.\u001b[39mgetfilesystemencoding()\n",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m: 'utf-8' codec can't decode byte 0xc7 in position 1: invalid continuation byte"
     ]
    }
   ],
   "source": [
    "import subprocess, sys\n",
    "import logging\n",
    "import re\n",
    "logging.basicConfig(\n",
    "    filename='docker_pthon_script.log',\n",
    "    level=logging.DEBUG\n",
    ")\n",
    "# 获取docker容器存放目录\n",
    "get_docker_dir_info_cmd = \"sudo docker info | grep 'Root'\"\n",
    "\n",
    "def get_docker_dir_info():\n",
    "    subprocess.check_output(get_docker_info_cmd, shell = True)\n",
    "    cmd_output_text = cmd_output.decode(shell_encoding)\n",
    "    \n",
    "# 查看docker运行状态\n",
    "get_docker_run_info_cmd = \"sudo systemctl status docker|grep -i 'Main PID'\"\n",
    "'''   Main PID: 2145698 (code=exited, status=0/SUCCESS)\n",
    "Active: inactive (dead) since Wed 2023-08-02 23:25:06 CST; 2 days ago\n",
    "Active: active (running) since Sat 2023-08-05 18:12:30 CST; 24s ago\n",
    "\n",
    "    return code标志\n",
    "'''\n",
    "def get_docker_run_info():\n",
    "    cmd_output = subprocess.check_output(get_docker_run_info_cmd, shell = True)\n",
    "    cmd_output_text = cmd_output.decode(shell_encoding)\n",
    "    \n",
    "    datepat = re.compile(r'(.+)\\((\\D+),(.+)\\)')\n",
    "    m = datepat.match(cmd_output_text)\n",
    "    docker_run_info = str(m.group(2))\n",
    "    return docker_run_info[docker_run_info.rfind('=')+1:]\n",
    "\n",
    "# 判断docker是否running    \n",
    "def docker_is_running():\n",
    "    docker_run_info_code = get_docker_run_info()\n",
    "    \n",
    "    if docker_run_info_code.__eq__('exited'):\n",
    "        return False\n",
    "    else:\n",
    "        logging.info('get docker run code is not exited, code = %r',docker_run_info_code)\n",
    "    return True\n",
    "\n",
    "    \n",
    "# 获取linux shell编码\n",
    "shell_encodings = ['utf8','gbk']\n",
    "def get_linux_shell_encoding():\n",
    "    global shell_encoding\n",
    "    cmd_output = subprocess.check_output(\"locale|grep 'LANG[^A-Z]'\", shell = True)\n",
    "    \n",
    "    for encoding in shell_encodings:\n",
    "        try:\n",
    "            cmd_output_text = cmd_output.decode('utf8')\n",
    "        except UnicodeDecodeError as e:\n",
    "            continue\n",
    "        shell_encoding = cmd_output_text[cmd_output_text.rfind('=')+1:]\n",
    "        break\n",
    "    if not shell_encoding:\n",
    "        logging.critical('linux shell encoding get error')\n",
    "    return shell_encoding\n",
    "\n",
    "'''\n",
    " 初始化处理，获取 shell 编码\n",
    "'''\n",
    "def __init__():\n",
    "    get_linux_shell_encoding()\n",
    "    \n",
    "def \n",
    "# cmd_output = subprocess.check_output(\"dir\", shell = True)\n",
    "\n",
    "\n",
    "# print (cmd_output.decode('utf8'))\n",
    "# sys.getfilesystemencoding()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43c675f0",
   "metadata": {},
   "source": [
    "#### script test\n",
    "这是我在学习编写docker脚本的测试文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "2ec8bd5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fun g:1\n",
      "global:1\n"
     ]
    }
   ],
   "source": [
    "from unittest import TestCase,main\n",
    "#测试\n",
    "class TestScript(TestCase):\n",
    "    def test_get_shell_encoding(self):\n",
    "        self.assertEqual(1,1)\n",
    "\n",
    "'''测试函数global关键字'''\n",
    "def test_fun_global():\n",
    "    global g\n",
    "    g=1\n",
    "    print (\"fun g:{g}\".format(g=g))\n",
    "test_fun_global()\n",
    "print ('global:{g}'.format(g=g))\n",
    "\n",
    "def __init__():\n",
    "    print ('init')\n",
    "# main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
